//
//  PictureTableViewCell.m
//  XMenSwift
//
//  Created by Jason on 2022/3/25.
//

#import "PictureTableViewCell.h"
#import <Masonry/Masonry.h>
#import "PictureCollectionViewCell.h"
#import "PicturePhotoAlbumTool.h"


@interface PictureTableViewCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collectionView;

@property (strong, nonatomic) UIView *bgView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *timeLabel;
@property (strong, nonatomic) UILabel *contenLabel;

@property (strong, nonatomic) UILabel *nameLabel;
@property (strong, nonatomic) UILabel *phoneLabel;

//如有需要把当前高度记录在模型中
@property (assign, nonatomic) double collectionViewHeight;

@end

#define screenNuber 4
#define itemSpacing 10
#define buttonHeight 100

@implementation PictureTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.maxPhotoNuber = 5;
        [self steupUi];
    }
    return self;
}

- (void)steupUi {
    
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    _bgView = bgView;
    [self.contentView addSubview:bgView];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor grayColor];
    line.alpha = 0.4;
    [self.contentView addSubview:line];
   
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16.0];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.text = @"优先级问题优优先级问题优优先级问题优";
    [bgView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.font = [UIFont systemFontOfSize:14.0];
    timeLabel.textColor = [UIColor grayColor];
    timeLabel.text = @"2020-08-09 14:45:40右边优先";
    [bgView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = [UIFont systemFontOfSize:16.0];
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.text = @"左边优先设置最大值这是名称看看最大内容是多少";
    [bgView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UILabel *phoneLabel = [[UILabel alloc] init];
    phoneLabel.font = [UIFont systemFontOfSize:16.0];
    phoneLabel.textColor = [UIColor grayColor];
    phoneLabel.text = @"18027231173";
    [bgView addSubview:phoneLabel];
    self.phoneLabel = phoneLabel;
    
    UILabel *contenLabel = [[UILabel alloc] init];
    contenLabel.font = [UIFont systemFontOfSize:14.0];
    contenLabel.textColor = [UIColor blackColor];
    contenLabel.text = @"这是内容啊 今天是个好天气这是内容啊 今天是个好天气这是内容啊 今天是个好天气这是内容啊 今天是个好天气这是内容啊 今天是个好天气这是内容啊 今天是个好天气";
    contenLabel.numberOfLines = 0;
    [bgView addSubview:contenLabel];
    self.contenLabel = contenLabel;
    
    [self setupCollectionView];
    [bgView addSubview:self.collectionView];

    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(15);
        make.bottom.mas_equalTo(-15);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(0);
    }];
    
   
    [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView);
        make.left.equalTo(bgView);
        make.height.mas_greaterThanOrEqualTo(20);
    }];
    
    [self.timeLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bgView);
        make.left.equalTo(titleLabel.mas_right).offset(10);
        make.right.equalTo(bgView);
        make.height.equalTo(titleLabel);
        //优先也必须设置最大值 否则另一边会被挤没
        make.width.mas_lessThanOrEqualTo(250);
    }];
    
    [self.nameLabel setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.left.equalTo(titleLabel);
        make.height.mas_greaterThanOrEqualTo(20);
        //优先也必须设置最大值 否则另一边会被挤没
        make.width.mas_lessThanOrEqualTo(200);

    }];
    
    [self.phoneLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel);
        make.left.equalTo(nameLabel.mas_right).offset(10);
        make.right.equalTo(bgView);
        make.height.equalTo(nameLabel);

    }];
    
    
    [self.contenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(10);
        make.left.equalTo(titleLabel);
        make.right.equalTo(bgView);
        make.bottom.equalTo(self.collectionView.mas_top).offset(-10);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.right.equalTo(bgView);
        make.bottom.equalTo(bgView);
        make.height.mas_equalTo(buttonHeight);

    }];
}

#pragma mark - 设置collection
-(void)setupCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 行间距
    layout.minimumLineSpacing = itemSpacing;
    // 设置cell之间的间距
    layout.minimumInteritemSpacing = itemSpacing;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collectionView = collectionView;
    [_collectionView setCollectionViewLayout:layout];
    //    _collectionView.backgroundColor = MainBackGroundColor;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.bounces = NO;
    _collectionView.pagingEnabled = NO;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.scrollEnabled = NO;
//    [_collectionView registerClass:[PictureCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([PictureCollectionViewCell class])];
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PictureCollectionViewCell class]) bundle:nil]forCellWithReuseIdentifier:NSStringFromClass([PictureCollectionViewCell class])];
}

-(void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    [self.collectionView reloadData];
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(200);
//        }];
//    });
    
    [self updateCollectionHeight];
    
}

- (void)selectPhoto {
    __weak typeof(self) weakSelf = self;
    PicturePhotoAlbumTool *tool =  [PicturePhotoAlbumTool sharedInstance];
    tool.saveType = PhotoSave;
    [tool showToolWithViewController:self.vc photoMaxCount:self.maxPhotoNuber photoCount:self.dataArray.count returnImageArrayBlock:^(NSArray<UIImage *> * _Nonnull imageUrlArray) {
        [weakSelf.dataArray addObjectsFromArray:imageUrlArray];
        [weakSelf.collectionView reloadData];
        [self updateCollectionHeight];
        
    }];
   
}

#pragma mark - 返回高度刷新
- (void)updateCollectionHeight {
  
    dispatch_async(dispatch_get_main_queue(), ^{
        //主线程刷新，
        [self.collectionView layoutIfNeeded];
        [self callBackCollectionHeight];
    });
}
- (void)callBackCollectionHeight {
    double currentHeight = ceil(self.collectionView.contentSize.height);
    if (_collectionViewHeight != currentHeight) {
        [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(currentHeight);
        }];
        if (self.collectionReloadHightBlock) {
            self.collectionReloadHightBlock();
        }
        _collectionViewHeight = currentHeight;

    }
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.dataArray.count >= self.maxPhotoNuber) {
        return self.maxPhotoNuber;
    }
    return self.dataArray.count + 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (@available(iOS 10.0, *)) {
        [collectionView setPrefetchingEnabled:NO];
    }
    PictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PictureCollectionViewCell class]) forIndexPath:indexPath];
    cell.addBgView.hidden = YES;
    cell.deleteButton.hidden = NO;
    if (indexPath.item == self.dataArray.count) {
        cell.addBgView.hidden = NO;
        cell.deleteButton.hidden = YES;
    } else {
        cell.imageView.image = self.dataArray[indexPath.item];
    }
    __weak typeof(self) weakSelf = self;
    cell.deleteBlock = ^{
        [weakSelf.dataArray removeObjectAtIndex:indexPath.item];
        [collectionView reloadData];
        [weakSelf updateCollectionHeight];
    };
    return cell;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (collectionView.bounds.size.width - (screenNuber - 1) * itemSpacing) / screenNuber - 0.1;
    return CGSizeMake(width,width);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item == self.dataArray.count) {
        [self selectPhoto];
    } else {
        //看大图
    }
}


@end
